﻿Ext.onReady(function () {
    Ext.apply(Kwan, {
        def: RoomStatus
    });
});
Ext.apply(Kwan, {
    blankClick: function (dv, e) {
        var group = e.getTarget("h2", 3, true);
        if (group) { group.up("div").toggleClass("collapsed"); }
    },
    roomClick: function (dv, nodes) {
        if (nodes.length > 0) {
            Kwan.def.RoomClick(nodes[0].id.replace('Room_', ''));
        }
    },
    ContextMenuClick: function (dataView, command) {
        var id = Kwan.selectedNodeID;
        var status = { 'miBroken': 2, 'miCleanRoom': 0, 'miDirtyRoom': 1 };
        switch (command) {
            case "miCheckIn": this.def.RoomClick(id); break;
            case "miCheckOut": this.def.RoomCheckOut(id); break;
            case "miChangeRoom": winChangeRoom.show(); break;
            case "miEditDeposit": this.def.EditDeposit(id); break;
            case "miCorrect": this.def.CorrectOrder(id); break;
            case "miCleanRoom":
            case "miBroken":
            case "miDirtyRoom": this.def.ChangeStatus(id, status[command]); break;
            default: this.def.InitClassInfo(id); break;
        }

    },
    showRoomTip: function (rowIndex) {
        alert(rowIndex);
        var roomInfo = roomStore.getAt(rowIndex).get('Items')[rowIndex];
        //var index = dataView.indexOf(this.triggerElement);
        alert(roomInfo['CustomerName']);
        RoomTip.setTitle(roomInfo['CustomerName'])
        //dataView.body.dom.innerHTML = '<b>Room :</b> ' + roomNo;
    },
    selectedNodeID: 0,
    customerName: '',
    rightClick: function (dataView, index, node, e) {
        Kwan.selectedNodeID = node.id.replace('Room_', '');
        if (!dataView.menu) {
            dataView.menu = RoomContextMenu;
        }
        dataView.menu.showAt(e.getXY());
        e.stopEvent();
    },
    submitPro: function (grid) {
        if (grid.store.data.length > 0) {
            grid.submitData();
        } else {
            this.def.ChangeProStatus();
        }
    },
    freeOrder: function (finalRoomPrice) {
        Ext.Msg.confirm('警告', "确定要免收并退房？<br>如果存在其他消费，只对其他消费结账。<br>按【否】还能避免，确认请按【是】",
            function (btn) {
                if (btn == 'yes') {
                    RoomStatus.FreeOrder(btnFinalPrice.text, gpConsume.getRowsValues());
                }
            });

    },
    changePay: function (cashPay, posPay, paid, obj) {
        cashPay = cashPay.length == 0 ? 0 : parseFloat(cashPay);
        posPay = posPay.length == 0 ? 0 : parseFloat(posPay);
        obj.setValue(cashPay + posPay - parseFloat(paid));
    }
});
Ext.apply(Kwan, {
    addPro: function (g1, g2) {
        if (g1.hasSelection()) {
            var d = g1.getRowsValues({ selectedOnly: true });
            g1.deleteSelected();
            //var d2 = [];
            //for (i = 0; i < d.length; i++) {
            //    d2.push({ EntityID: 0, Item: d[i].NameCN, Price: d[i].Price, Total: 1 });
            //}
            //Kwan.Debug(d2);
            //g2.store.data.add(d2);
            this.def.AddPro(d);
        }
    },
    add: function (source, destination) {
        if (source.hasSelection()) {
            //destination.submitData();
            var records = source.selModel.getSelections();
            //this.def.(records[0]);
            source.deleteSelected();
            destination.store.add(records);
        }
    },
    addAll: function (source, destination) {
        var records = source.store.getRange();
        source.store.removeAll();
        destination.store.add(records);
    },
    addByName: function (name) {
        if (!Ext.isEmpty(name)) {
            var result = ProductDS1.query("Name", name);

            if (!Ext.isEmpty(result.items)) {
                gpProduct1.store.remove(result.items[0]);
                gpConsume.store.add(result.items[0]);
            }
        }
    },
    addByNames: function (name) {
        for (var i = 0; i < name.length; i++) {
            this.addByName(name[i]);
        }
    },
    remove: function (source, destination) {
        this.add(destination, source);
    },
    removeAll: function (source, destination) {
        this.addAll(destination, source);
    }
});